package com.app.interceptor;


import cn.dev33.satoken.stp.StpUtil;
import com.app.exception.BaseException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@Component
public class SpringbootInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {

    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //刷新过期时间
        String token = request.getHeader("token");
         //++logger.info(request.getRequestURI());
         //++logger.info(request.getQueryString());

        List<String> openPaths = Arrays.asList(
                "/api/adminUser/captcha"
        );

        //物联网设备推送接口放行
        String uri = request.getRequestURI();

        System.err.println("当前请求uri: " + uri);

        //登录接口，放行
        if (uri.contains("/login") || uri.contains("getUserCode") || uri.contains("oauth2")) {
            return true;
        }

        //文件，放行
        if (uri.contains("/file/") || uri.contains("/template/")){
            return true;
        }

        if (!StpUtil.isLogin()) {
            throw new BaseException(20001, "用户未登录");
        }


        //PC端纵向越权,以下路径为PC端调用
        if (openPaths.contains(uri)){
            return true;
        }

        throw new BaseException(20001, "无权限访问");
    }

}